package com.yunhe.device.repository.base.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yunhe.common.util.StringUtil;
import com.yunhe.device.domain.base.DeviceType;
import com.yunhe.device.model.base.EquipmentTypeQueryCondition;
import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName EquipmentTypeDao
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/15 14:36
 * @Version 1.0
 **/
@Repository
public class EquipmentTypeDao {

    @Autowired
    private Session session;

    public Page<DeviceType> getEquipmentTypeByCondition(EquipmentTypeQueryCondition equipmentTypeQueryCondition) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("MATCH (et:DeviceType) WHERE 1=1 and  et.stationId is null ");
        //关键字查询
        if(!StringUtil.isEmpty(equipmentTypeQueryCondition.getKeywords())){
            cql.append(" AND (et.name=~'.*"+equipmentTypeQueryCondition.getKeywords()+".*'");
            cql.append(" or et.title=~'.*"+equipmentTypeQueryCondition.getKeywords()+".*')");
        }
        int skip=(equipmentTypeQueryCondition.getPage()-1)*equipmentTypeQueryCondition.getSize();
        String queryEnd=" RETURN et" +
                "  order by et.createTime desc  SKIP "+skip+" LIMIT "+equipmentTypeQueryCondition.getSize();
        String countEnd=" RETURN count(et)";

        //分页数据查询结果
        Iterable<DeviceType> equipmentTypes = session.query(DeviceType.class,cql.toString()+queryEnd, Maps.newHashMap());
        //查询总数
        Integer total = session.queryForObject(Integer.class,cql.toString()+countEnd, Maps.newHashMap());

        //封装为分页类
        List<DeviceType> content= Lists.newArrayList(equipmentTypes);
        Pageable pageable= PageRequest.of(equipmentTypeQueryCondition.getPage()-1,equipmentTypeQueryCondition.getSize());
        Page<DeviceType> page=new PageImpl<DeviceType>(content,pageable,total);
        return page;
    }

    public Page<DeviceType> getFrontEquipmentTypeByCondition(EquipmentTypeQueryCondition equipmentTypeQueryCondition) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("MATCH (et:DeviceType) WHERE 1=1 ");
        //关键字查询
        cql.append(" AND (et.stationId is null ");
        cql.append(" or et.stationId= "+equipmentTypeQueryCondition.getStationId()+")");
        if(!StringUtil.isEmpty(equipmentTypeQueryCondition.getKeywords())){
            cql.append(" AND (et.name=~'.*"+equipmentTypeQueryCondition.getKeywords()+".*'");
            cql.append(" or et.title=~'.*"+equipmentTypeQueryCondition.getKeywords()+".*')");
        }
        int skip=(equipmentTypeQueryCondition.getPage()-1)*equipmentTypeQueryCondition.getSize();
        String queryEnd=" RETURN et" +
                "  order by et.equipCount desc, et.pySn asc  SKIP "+skip+" LIMIT "+equipmentTypeQueryCondition.getSize();
        String countEnd=" RETURN count(et)";

        //分页数据查询结果
        Iterable<DeviceType> equipmentTypes = session.query(DeviceType.class,cql.toString()+queryEnd, Maps.newHashMap());
        //查询总数
        Integer total = session.queryForObject(Integer.class,cql.toString()+countEnd, Maps.newHashMap());

        //封装为分页类
        List<DeviceType> content= Lists.newArrayList(equipmentTypes);
        Pageable pageable= PageRequest.of(equipmentTypeQueryCondition.getPage()-1,equipmentTypeQueryCondition.getSize());
        Page<DeviceType> page=new PageImpl<DeviceType>(content,pageable,total);
        return page;
    }
}
